<?php
class UsersController extends CController {
	public $defaultAction = 'bulkmails';
	
	public function filters() {
		return array(
				'accessControl'
		);
	}

	public function accessRules() {
		return array(
			array(
				'allow',
				'expression' => ForumMembers::isAdmin()
			),
			array(
				'deny',
			),
		);
	}

	public function actionBulkmails() {
		$bulkMail = new BulkmailForm();
		if(isset($_POST['BulkmailForm'])) {
			$bulkMail->attributes = $_POST['BulkmailForm'];
			if($bulkMail->validate()) {
				Yii::app()->user->setFlash('bulkMail', serialize($bulkMail));
				$this->redirect(array('/admin/users/sendmails'));
			}
		}

		$this->render('create-bulkmail', array('bulkMail' => $bulkMail));
	}
	
	public function actionSendMails() {
		if(Yii::app()->user->hasFlash('bulkMail')) {
			$bulkMail = unserialize(Yii::app()->user->getFlash('bulkMail'));
			if($bulkMail) {
				$criteria = $bulkMail->getUserCriteria();
				$mailsCount = ForumMembers::model()->with('bbs')->count($criteria);
				Yii::app()->user->setState('mailsCount', $mailsCount);
				Yii::app()->user->setState('mailsSent', 0);
				$this->render('send-mails', array('bulkMail' => $bulkMail));
			}
		} else {
			die('读取邮件错误，请重新撰写发送。');
		}
	}
	
	public function actionStartToSendMail() {
		if(Yii::app()->user->hasFlash('bulkMail')) {
			$bulkMail = unserialize(Yii::app()->user->getFlash('bulkMail'));
			if($bulkMail) {
				$criteria = $bulkMail->getUserCriteria();
				$users = ForumMembers::model()->with('bbs')->findAll($criteria);
				foreach($users as $user) {
					if(sendMail($user->email, $user->username, $bulkMail->title, $bulkMail->content)) {
						Yii::app()->user->setState('mailsSent', (int)Yii::app()->user->getState('mailsSent')+1);
					}
				}
			}
		}
	}
	
	public function actionUpdatesendingprocess() {
		ini_set('implicit_flush', 'On');
		ini_set('output_buffering', '0');
		$this->widget('AdminSendMailProcess');
	}

	/**
	public function actionManage() {
		$criteria = new CDbCriteria;
		$count = Users::model()->count($criteria);
		// Pagination
		$pages = new CPagination($count);
		$pages->pageSize = Yii::app()->params['manageListPerPage'];
		$pages->applyLimit($criteria);
		// Get rows
		$users = Users::model()->findAll($criteria);
		
		$this->render('usersManage', array(
				'pages' => $pages,
				'users' => $users,
				'count' => $count
			)
		);
	}
	

	public function actionToggleStatus() {
		$userId = isset($_GET['id']) ? (int)$_GET['id'] : -1;

		if($userId) {
			$user = Users::model()->findByPk($userId);
			if($user !== null) {
				if($user->userStatus == Users::STATUS_ENABLE) {
                                    $user->userStatus = Users::STATUS_DISABLE;
				} else if($user->userStatus == Users::STATUS_DISABLE) {
                                    $user->userStatus = Users::STATUS_ENABLE;
				}
				if($user->save()) {
					$message = '用户状态设置成功。';
					Yii::app()->user->setFlash('message', $message);
					$this->redirect(Yii::app()->request->urlReferrer);
				}
			}
		}
	}


        public function actionToggleRole() {
		$userId = isset($_GET['id']) ? (int)$_GET['id'] : -1;

		if($userId) {
			$user = Users::model()->findByPk($userId);
			if($user !== null) {
				if($user->userRole == Users::ROLE_ADMIN) {
                                    $user->userRole = Users::ROLE_USER;
				} else if($user->userRole == Users::ROLE_USER) {
                                    $user->userRole = Users::ROLE_ADMIN;
				}
				if($user->save()) {
					$message = '用户角色设置成功。';
					Yii::app()->user->setFlash('message', $message);
					$this->redirect(Yii::app()->request->urlReferrer);
				}
			}
		}
	} */
}